import { createRouter, createWebHistory, createWebHashHistory } from 'vue-router';
import routes from './routes';
import beforeEach from './beforeEach';
import afterEach from './afterEach';
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';

//进度条属性配置
NProgress.configure({
    easing: 'ease', // 动画方式
    speed: 500, // 递增进度条的速度
    showSpinner: true, // 是否显示加载 icon
    trickleSpeed: 200, // 自动递增间隔
    minimum: 0.3 // 初始化时的最小百分比
});

// 构造路由实例
const router = createRouter({
    history: createWebHashHistory(),
    routes,
    scrollBehavior(_to, _from, savedPosition) {
        return savedPosition || { top: 0 };
    }
});
//全局前置守卫
router.beforeEach((to, from, next) => {
    NProgress.start(); // 进度条开始
    beforeEach.forEach((item) => item(to, from, next));
});
//全局后置守卫
router.afterEach((to, from, failure) => {
    afterEach.forEach((item) => item(to, from, failure));
    NProgress.done(); // 进度条结束
});
export default router;
